/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.prozezuak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import dBasea.Bidaiariak;

public class BidaiariaBete extends Panela {

	private static final long serialVersionUID = 1L;
	private JLabel bezNif;
	private JTextField nif;
	private JComponent izena;
	private JTextField ize;
	private JLabel abizenak;
	private JTextField abi;
	private JLabel telefonoa;
	private JTextField tele;
	private JButton ok;
	private JButton ezeztatu;
	private JButton btnBilatu;
	private Bidaiariak bida = new Bidaiariak();

	public BidaiariaBete(String tituloa, final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);
		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout(0, 0));

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);

		JPanel xxx = new JPanel();
		xxx.setBackground(SystemColor.activeCaption);
		erdikoa.add(xxx);

		JPanel xxxx = new JPanel();
		xxxx.setBackground(SystemColor.activeCaption);
		erdikoa.add(xxxx);

		JPanel xx = new JPanel();
		xx.setBackground(SystemColor.activeCaption);
		bezNif = new JLabel("Bidaiari NIF:");
		bezNif.setFont(new Font("Arial", Font.PLAIN, 14));
		nif = new JTextField(20);
		nif.addKeyListener(new KeyAdapter() {// limitea NAN

			public void keyTyped(KeyEvent arg0) {
				if (nif.getText().length() >= 10) {
					arg0.consume();
				}
			}
		});
		xx.add(bezNif);
		xx.add(nif);
		erdikoa.add(xx);

		erdikoa.add(xx);

		btnBilatu = new JButton("Bilatu");
		btnBilatu.setIcon(new ImageIcon(BidaiariaBete.class
				.getResource("/Argazkia/lupa2.gif")));
		xx.add(btnBilatu);

		JPanel arrib = new JPanel();
		arrib.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arrib = new FlowLayout();
		fl_arrib.setHgap(15);
		fl_arrib.setVgap(9);
		arrib.setLayout(fl_arrib);
		izena = new JLabel("Izena:");
		izena.setFont(new Font("Arial", Font.PLAIN, 14));
		ize = new JTextField(20);
		arrib.add(izena);
		arrib.add(ize);
		erdikoa.add(arrib);

		JPanel erdi = new JPanel();
		erdi.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdi = new FlowLayout();
		fl_erdi.setHgap(15);
		fl_erdi.setVgap(8);
		erdi.setLayout(fl_erdi);
		abizenak = new JLabel("Abizenak:");
		abizenak.setFont(new Font("Arial", Font.PLAIN, 14));
		abi = new JTextField(20);
		erdi.add(abizenak);
		erdi.add(abi);
		erdikoa.add(erdi);

		JPanel bost = new JPanel();
		bost.setBackground(SystemColor.activeCaption);
		FlowLayout fl_bost = new FlowLayout();
		fl_bost.setHgap(15);
		fl_bost.setVgap(8);
		bost.setLayout(fl_bost);
		telefonoa = new JLabel("Telefonoa:");
		telefonoa.setFont(new Font("Arial", Font.PLAIN, 14));
		telefonoa.setVerticalAlignment(SwingConstants.BOTTOM);
		try {
			tele = new JFormattedTextField(new MaskFormatter("#########"));
			tele.setColumns(12);
		} catch (ParseException e2) {

			e2.printStackTrace();
		}

		bost.add(telefonoa);
		bost.add(tele);
		erdikoa.add(bost);
		bost.setBackground(SystemColor.activeCaption);

		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(8, 1));
		JPanel gs = new JPanel();
		gs.setBackground(SystemColor.activeCaption);
		erdikoa.add(gs);

		FlowLayout fl_behekoa = new FlowLayout();
		fl_behekoa.setVgap(20);
		fl_behekoa.setHgap(110);
		JPanel behekoa = new JPanel(fl_behekoa);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Bidaiaria sortu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");

		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);

		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		ize.setEnabled(false);
		abi.setEnabled(false);
		tele.setEnabled(false);
		btnBilatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					// eremu hutsak zerbait idatzi bada
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					}// existitzen bada istripua
					else if (!bida.dagoBidaiaria(nif.getText())) {
						JOptionPane.showMessageDialog(null,
								"Ez da bidaiari hori aurkitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(null,
								"Bidaiari hori aurkitu da!", "Ondo",
								JOptionPane.INFORMATION_MESSAGE);
						nif.setEnabled(false);
						ize.setEnabled(true);
						abi.setEnabled(true);
						tele.setEnabled(true);

					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

		});

		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaiIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (!bida.dagoBidaiaria(nif.getText())) {
						JOptionPane.showMessageDialog(null,
								"Bidaiaria hau ez daexistitzen  \n"
										+ "Sartu NAN egokia duen pertsona",
								"Oker", JOptionPane.ERROR_MESSAGE);
					}

					else {
						try {
							int tfnoa = Integer.parseInt(tele.getText());
							bida.bidUpdate(nif.getText(), ize.getText(),
									abi.getText(), tfnoa);
							JOptionPane.showMessageDialog(null,
									" Ondo sortu da bidaiaria", "",
									JOptionPane.INFORMATION_MESSAGE);
							nif.setEnabled(true);
							ize.setEnabled(false);
							abi.setEnabled(false);
							tele.setEnabled(false);
							eremuakGarbitu();
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu telefonoko eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
				nif.setEnabled(true);
				ize.setEnabled(false);
				abi.setEnabled(false);
				tele.setEnabled(false);
			}
		});
	}

	private boolean zerbaitIdatzi() {
		boolean aurki;
		aurki = nif.getText().length() > 0;
		return aurki;
	}

	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki = nif.getText().length() > 0 && ize.getText().length() > 0
				&& abi.getText().length() > 0 && tele.getText().length() > 0;
		return aurki;
	}

	public void eremuakGarbitu() {
		nif.setText("");
		ize.setText("");
		abi.setText("");
		tele.setText("");
	}

}
